package com.glasses.manage.auth.controller;

import com.glasses.manage.auth.entity.Words;
import com.glasses.manage.auth.service.BECTest2Service;
import com.glasses.manage.auth.service.BECTestService;
import com.glasses.manage.core.entity.PageBean;
import com.glasses.manage.core.ezpage.EzPageParam;
import com.glasses.manage.core.ezpage.PageUtils;
import com.glasses.manage.utils.JSONUtil;
import com.glasses.manage.utils.MessageBox;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

/**
 * @Auther: yuyang
 * @Date: 2019/7/1 15:15
 * @ClassName:BECTestController
 * @Description: TODO
 */
@RequestMapping(value = {"/becTest"})
@Controller
public class BECTestController {

    //定义静态常量，用于切换数据表
    private static  String TABLE_NAME="t_words";

    @Autowired
    private BECTestService becTestService;

    @Autowired
    private BECTest2Service becTest2Service;


    /**
     * 跳转bec页面
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String manage_index() {
        return "BEC_test";
    }


    /**
     * 查询出所有的单词
     * @param pageParam
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getBecList(EzPageParam pageParam) throws Exception{
        PageBean<Words> words=new PageBean<>();
        if (TABLE_NAME.equals("t_words")){
            words = becTestService.getAll(PageUtils.convert(pageParam));
        }else {
            words = becTest2Service.getAll(PageUtils.convert(pageParam));
        }
        return JSONUtil.object2Json(PageUtils.convertPageBean(words));
    }


    /**
     * 数据库随机获取四条记录用于单词选择测试或者单词拼写测试
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/test", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getBecTest() throws Exception{
        try{
            if (TABLE_NAME.equals("t_words")){
                Long count=becTestService.getCount();
                if (count==null || count<1){
                    return JSONUtil.object2Json(MessageBox.getErrorMsg("未掌握单词不足1个！"));
                }
                List<Words> words = becTestService.getOne();
                return JSONUtil.object2Json(words);
            }else {
                Long count=becTest2Service.getCount();
                if (count==null || count<1){
                    return JSONUtil.object2Json(MessageBox.getErrorMsg("未掌握单词不足1个！"));
                }
                List<Words> words = becTest2Service.getOne();
                return JSONUtil.object2Json(words);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 校验单词测试正确性
     * @param questId
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/check1", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String check1(Long questId) throws Exception{
        Words words=new Words();
        words.setId(questId);
        words.setState(1);
        if (TABLE_NAME.equals("t_words")){
            becTestService.update1(words);
        }else {
            becTest2Service.update1(words);
        }
        return JSONUtil.object2Json(words);
    }

    /**
     * 重置选择的表的所有数据
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/reset", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String reset() throws Exception{
        Words words=new Words();
        words.setState(0);
        if (TABLE_NAME.equals("t_words")){
            becTestService.update1(words);
        }else {
            becTest2Service.update1(words);
        }

        return JSONUtil.object2Json(MessageBox.getSuccessMsg("重置成功"));
    }


    /**
     * 切换单词数据表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/changeTable", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String changeTable() throws Exception{
        if (TABLE_NAME.equals("t_words")){
            TABLE_NAME="t_words2";
        }else {
            TABLE_NAME="t_words";
        }

        return JSONUtil.object2Json(MessageBox.getSuccessMsg("切换成功，当前表为："+TABLE_NAME));
    }

    /**
     * 导入excel
     * 采用file.Transto 来保存上传的文件
     *
     * @return
     */
    @RequestMapping("/importExcel")
    @ResponseBody
    public MessageBox importexcel(MultipartFile planFile) {
        String str = null;
        //通过MultipartFile的方法直接写文件
        try {
            if (TABLE_NAME.equals("t_words")){
                str = becTestService.importExcel(planFile.getInputStream());
            }else {
                str = becTest2Service.importExcel(planFile.getInputStream());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        if (str.equals("success")) {
            return MessageBox.getSuccessMsg("导入成功");
        } else {
            return MessageBox.getWarningMsg(str);
        }
    }

}
